package com.robot.merchant.service;

import com.robot.merchant.dao.TagDao;
import com.robot.merchant.pojo.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.IdWorker;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * tag服务层
 *
 * @author
 */
@Service
public class TagService {

    @Autowired
    private TagDao tagDao;

    @Autowired
    private IdWorker idWorker;

    /**
     * 查询全部列表
     *
     * @return
     */
    public List<Tag> findAll() {
        return tagDao.findAll();
    }


    /**
     * 条件查询+分页
     *
     * @param whereMap
     * @param page
     * @param size
     * @return
     */
    public Page<Tag> findSearch(Map whereMap, int page, int size) {
        Specification<Tag> specification = createSpecification(whereMap);
        PageRequest pageRequest = PageRequest.of(page - 1, size);
        return tagDao.findAll(specification, pageRequest);
    }


    /**
     * 条件查询
     *
     * @param whereMap
     * @return
     */
    public List<Tag> findSearch(Map whereMap) {
        Specification<Tag> specification = createSpecification(whereMap);
        return tagDao.findAll(specification);
    }

    /**
     * 根据ID查询实体
     *
     * @param id
     * @return
     */
    public Tag findById(String id) {
        return tagDao.findById(id).get();
    }

    /**
     * 增加
     *
     * @param tag
     */
    public void add(Tag tag) {
        //雪花分布式ID生成器
        tag.setId(idWorker.nextId() + "");
        tagDao.save(tag);
    }

    /**
     * 修改
     *
     * @param tag
     */
    public void update(Tag tag) {
        tagDao.save(tag);
    }

    /**
     * 删除
     *
     * @param id
     */
    public void deleteById(String id) {
        tagDao.deleteById(id);
    }

    /**
     * 动态条件构建
     *
     * @param searchMap
     * @return
     */
    private Specification<Tag> createSpecification(Map searchMap) {
        return new Specification<Tag>() {
            @Override
            public Predicate toPredicate(Root<Tag> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<Predicate>();
                // 标签id
                String id = "id";
                if (searchMap.get(id) != null && !"".equals(searchMap.get(id))) {
                    predicateList.add(cb.like(root.get(id).as(String.class), "%" + (String) searchMap.get(id) + "%"));
                }
                // 标签内容
                String content = "content";
                if (searchMap.get(content) != null && !"".equals(searchMap.get(content))) {
                    predicateList.add(cb.like(root.get(content).as(String.class), "%" + (String) searchMap.get(content) + "%"));
                }
                //剔除软删除数据
                String deletedAt = "deletedAt";
                predicateList.add(cb.equal(root.get(deletedAt), cb.nullLiteral(Date.class)));

                return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
    }

    /**
     * 通过storeId查找标签列表
     *
     * @param storeId
     * @return
     */
    public List<Tag> findTagsByStoreId(String storeId) {
        return tagDao.findTagsByStoreId(storeId);
    }
}
